package com.codeoss.framework.webide.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.util.IOUtil;
import com.codeoss.framework.util.ValueNamePair;
import com.codeoss.framework.webide.app.service.impl.AppExportManager;
import com.codeoss.framework.webide.app.service.impl.AppImportManager;
import com.codeoss.framework.webide.request.ApplicationPageRequest;
import com.codeoss.framework.webide.response.ApplicationPageResponse;
import com.codeoss.framework.webide.service.IApplicationPageService;
import com.codeoss.module.ir.model.IrApplication;
import com.codeoss.module.ir.service.IIrApplicationService;
import com.codeoss.module.ir.service.IIrDictService;

@Service
public class ApplicationPageService implements IApplicationPageService {
	@Value("${framework.base-path}")
	private String basePath;
	@Autowired
	private IIrApplicationService irApplicationService;
	  @Autowired
	  private AppExportManager appExportManager;
	  @Autowired
	  private AppImportManager appImportManager;
	  @Autowired
		private IIrDictService irDictService;
	@Override 
	public ApplicationPageResponse init() {
		// TODO Auto-generated method stub
		ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
		QueryWrapper<IrApplication> applicationQuery = new QueryWrapper<IrApplication>();
		applicationQuery.orderByAsc("id");
		List<IrApplication> applicationList = irApplicationService.list(applicationQuery);
		applicationPageResponse.setApplicationList(applicationList);
		
		List<ValueNamePair> moduleTypeList=irDictService.getPairListByCode("moduleType");
		applicationPageResponse.setModuleTypeList(moduleTypeList);
		
		return applicationPageResponse;
	}

	@Override
	@Transactional
	public ApplicationPageResponse dataSave(ApplicationPageRequest request) {
		// TODO Auto-generated method stub
		ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
		Long id = request.getId();
		IrApplication irApplication = null;
		if (id != null) {
			irApplication = irApplicationService.getById(id);
		} else {
			irApplication = new IrApplication();
		}
		irApplication.setCode(request.getCode());
		irApplication.setName(request.getName());
		irApplication.setDescription(request.getDescription());
		irApplication.setModuleTypeList(request.getModuleTypeList());
		irApplication.setIcon(request.getIcon());
		if (id != null) {
			irApplicationService.update(irApplication);
		} else {
			irApplicationService.insert(irApplication);
		}
		return applicationPageResponse;
	}

	@Transactional
	public ApplicationPageResponse dataImport(InputStream is) {
       ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
       YunContext yunContext=YunContext.getCurrentContext();
		appImportManager.importApp(yunContext, is);
		return applicationPageResponse;
	}
	
	public ApplicationPageResponse dataExport(String code) {
		ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		IrApplication irApplication=irApplicationService.getIrApplicationByCode(code);
		ByteArrayOutputStream os= appExportManager.exportApp(yunContext, irApplication);
		applicationPageResponse.setByteArrayOutputStream(os);
		return applicationPageResponse;
	}

	@Override
	@Transactional
	public ApplicationPageResponse dataDelete(String code) {
		// TODO Auto-generated method stub
		ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
		IrApplication irApplication=irApplicationService.getIrApplicationByCode(code);
		irApplicationService.delete(irApplication);
		return applicationPageResponse;
	}

	@Override
	public ApplicationPageResponse getApplicationIconContent(String icon) {
		// TODO Auto-generated method stub
		ApplicationPageResponse applicationPageResponse = new ApplicationPageResponse();
		if(icon.startsWith("classpath:")) {
			icon=icon.substring("classpath:".length());
			InputStream is = this.getClass().getClassLoader().getResourceAsStream(icon);
			ByteArrayOutputStream os=new ByteArrayOutputStream();
			byte b[]=new byte[1024];
			int len=0;
			try {
				while((len=is.read(b))!=-1) {
					os.write(b, 0, len);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}finally {
				IOUtil.closeInputStream(is);
			}
			applicationPageResponse.setApplicationIconContent(os.toByteArray());
		}
		return applicationPageResponse;
	}
}
